<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


class JFX_Data
{
	public static function getFile($filepath, $filename, $moduleKey){
		$file = new JFX_Data_File($filepath, $filename, $moduleKey);
		if(!empty($file->meta['filename']) && file_exists($file->getAbsolutePath())){
			return $file;
		}
	}

	public static function newFile($moduleKey, $filepath, $friendlyName='', $prefix=''){
		$file = new JFX_Data_File($filepath);
		$file->setFile($moduleKey, $filepath, $friendlyName, $prefix);
		return $file;
	}

	public static function getAllModuleFiles($moduleKey){
		$DB = JFX::registry('db');
		$CONFIG = JFX::registry('config');
		$ids = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}data WHERE module_key = '{$moduleKey}'");
		foreach($ids as $k=>$row){
			$ids[$k] = new JFX_Data_File($row['filepath'], $row['filename'], $moduleKey);
		}
		return $ids;
	}

	public static function deleteAllFiles($moduleKey){
		$allFiles = self::getAllModuleFiles($moduleKey);
		foreach($allFiles as $k=>$file){
			$file->delete();
		}
	}

	public static function getModuleFilesByPrefix($moduleKey, $prefix=''){
		$DB = JFX::registry('db');
		$CONFIG = JFX::registry('config');
		$ids = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}data WHERE module_key = '{$moduleKey}' AND filepath = '{$prefix}'");
		foreach($ids as $k=>$row){
			$ids[$k] = new JFX_Data_File($row['filepath'], $row['filename'], $moduleKey);
		}
		return $ids;
	}

	public static function getFileByAbsolute($path, $moduleKey){
		$DB = JFX::registry('db');
		$CONFIG = JFX::registry('config');
		$path = str_replace('DIRECTORY_SEPARATOR', '/', $path);
		$pathParts = explode('/', $path);
		$filename = array_pop($pathParts);
		$filepath = implode('/', $pathParts);
		$filepath = ltrim(str_replace($CONFIG->dataDir.'/'.$moduleKey, '', $filepath), '/');
		return self::getFile($filepath, $filename, $moduleKey);
	}

	public static function compress($data){
		return $data;
	}

	public static function inflate($data){
		return $data;
	}

}






